package com.wx.lambda.ii.chapter11;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;


/**
 * 第11章测试
 * CompletableFuture
 */
@Slf4j
public class Chapter11Test {
    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(2);
        FutureTask futureTask = new FutureTask(()->LongStream.range(0, 999999999L).boxed().reduce(0L, Long::sum));

        executorService.submit(futureTask);
        Long sum = LongStream.range(0, 9999999L).boxed().parallel().reduce(0L, Long::sum);
        log.info("sum={}", sum);

        try {

            Object o = futureTask.get(10, TimeUnit.SECONDS);
            log.info("o={}", o);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            log.info("是否执行完成：{}",futureTask.isDone());
            futureTask.cancel(true);
            log.info("是否取消：{}",futureTask.isCancelled());
            throw new RuntimeException(e);
        } finally {
            executorService.shutdown();
        }

    }


}
